import React, { ReactElement } from 'react';
import codePush, { RemotePackage } from 'react-native-code-push';
import { NavigationActions } from 'react-navigation';

import { CODE_PUSH_KEY } from '../../constant';
import { modalOpen, modalClose, loading, toolTipOpen } from '../../util/global';
import UpdateProgress from '../../components/UpdateProgress';
import PortraitAlert from '../../components/Alert/PortraitAlert';
import { LoginApi } from '../../api';
import { clearUserInfo } from '../../store/user';
import { destroyEngine } from '../../util/RTC/rtcEngine';
import navigator from '../../util/navigator';

const checkForUpdate = (): Promise<any> => {
  return new Promise((resolve, reject): void => {
    const checkTimer = setTimeout((): void => {
      reject();
    }, 10 * 1000);

    codePush
      .checkForUpdate(CODE_PUSH_KEY)
      .then((update: RemotePackage | null): void => {
        clearTimeout(checkTimer);
        if (update) {
          resolve(update);
        } else {
          reject();
        }
      })
      .catch((): void => {
        clearTimeout(checkTimer);
        reject();
      });
  });
};

const downLoadPackage = (update: RemotePackage): void => {
  modalOpen({
    Type: (): React.ReactElement => (
      <UpdateProgress
        update={update}
        packageSize={update.packageSize}
        download={(): void => downLoadPackage(update)}
      />
    ),
    needBack: true
  });
};

export const packageUpdate = (update: RemotePackage): void => {
  modalOpen({
    Type: (): React.ReactElement => (
      <PortraitAlert
        title="检测到新版本"
        sureDesc="立即更新"
        cancelDesc="暂不更新"
        subTitle={`检测到有一个新版本可以更新\n更新包大小共计${(
          update.packageSize /
          1024 /
          1024
        ).toFixed(2)}MB`}
        sureAction={(): void => {
          downLoadPackage(update);
        }}
        cancelAction={modalClose}
      />
    ),
    needBack: true
  });
};

export const checkUpdate = (): void => {
  loading.show();
  checkForUpdate()
    .then((update: RemotePackage): void => {
      loading.hide();
      packageUpdate(update);
    })
    .catch((): void => {
      loading.hide();
      toolTipOpen({ desc: '未检测到新版本' });
    });
};

export const logout = (): void => {
  LoginApi.logout().then((): void => {
    clearUserInfo();
    destroyEngine();
    navigator.reset([NavigationActions.navigate({ routeName: 'Login' })]);
  });
};

export const logoutAlert = (): void => {
  modalOpen({
    Type: (): React.ReactElement => (
      <PortraitAlert
        title="退出账号"
        sureDesc="退出账号"
        cancelDesc="取消"
        subTitle="确定要退出当前帐号吗？"
        sureDescStyle={{ color: '#F57272' }}
        sureAction={(): void => {
          modalClose();
          logout();
        }}
        cancelAction={modalClose}
      />
    ),
    needBack: true
  });
};

export const onPressPasswordSet = (): void => {
  navigator.push('PasswordSet');
};

export const onPressDownload = (): void => {
  navigator.push('DownloadManage');
};
